/*
  	Arkodroid - An Arduino Controller for Android
	Copyright (c) 2013 Miguel Monteiro, Eduardo Peixoto, Rui Pedro Carvalhal, André Braga.
	All rights reserved.
	http://code.google.com/p/arkodroid/
	
	Amarino API by Bonifaz Kaufmann - http://www.amarino-toolkit.net/
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package net.miguelmonteiro.android.arkodroid;

import net.miguelmonteiro.android.arkodroid.Checker.Resource;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import at.abraxas.amarino.Amarino;

public class ArkodroidButtons extends Activity {    
	// Variables
	long lastchanged;
	
	public static final String TAG = "ArkodroidButtons";
	
	// Declare objects
	TextView xCoor;
	TextView yCoor;

	ImageButton upButton;
	ImageButton downButton;
	ImageButton leftButton;
	ImageButton rightButton;
    Button buzzerButton;
    ToggleButton toggleButton;
	
    /** Check if Bluetooth is ON **/
	@Override 
    protected void onResume() {
		super.onResume();
		
    	new Checker(this).pass(new Checker.Pass() {
    		@Override public void pass() {
    	    	// Nothing
    	 	}
    	}).check(Resource.BLUETOOTH);
    }

    /** Called when the activity is first created. **/
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main_buttons);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        //Arkodroid.ACTIVITY = this;
        lastchanged = System.currentTimeMillis();
        
        // Create objects
        xCoor = (TextView)findViewById(R.id.xcoor);
		yCoor = (TextView)findViewById(R.id.ycoor);
		upButton = (ImageButton) findViewById(R.id.imageButtonUp);
		downButton = (ImageButton) findViewById(R.id.imageButtonDown);
		leftButton = (ImageButton) findViewById(R.id.imageButtonLeft);
		rightButton = (ImageButton) findViewById(R.id.imageButtonRight);
		buzzerButton = (Button) findViewById(R.id.buttonBuzzer);
	    
		upButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					yCoor.setText("Y: -10");
					Arkodroid.values[0] = "-10";
					Arkodroid.sendValues(ArkodroidButtons.this);
	            } else if (event.getAction() == MotionEvent.ACTION_UP) {
	            	yCoor.setText("Y: 0");
	            	Arkodroid.values[0] = "0";
	    			Arkodroid.sendStopValues(ArkodroidButtons.this);
	            }
				return false;
			}
	    });
		
		downButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	yCoor.setText("Y: 10");
		        	Arkodroid.values[0] = "10";
					Arkodroid.sendValues(ArkodroidButtons.this);
	            } else if (event.getAction() == MotionEvent.ACTION_UP) {
	            	yCoor.setText("Y: 0");
	            	Arkodroid.values[0] = "0";
	    			Arkodroid.sendStopValues(ArkodroidButtons.this);
	            }
				return false;
			}
	    });
		
		leftButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	xCoor.setText("X: -10");
		        	Arkodroid.values[1] = "-10";
					Arkodroid.sendValues(ArkodroidButtons.this);
	            } else if (event.getAction() == MotionEvent.ACTION_UP) {
	            	xCoor.setText("X: 0");
	            	Arkodroid.values[1] = "0";
	    			Arkodroid.sendStopValues(ArkodroidButtons.this);
	            }
				return false;
			}
	    });
		
		rightButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	xCoor.setText("X: 10");
		        	Arkodroid.values[1] = "10";
					Arkodroid.sendValues(ArkodroidButtons.this);
	            } else if (event.getAction() == MotionEvent.ACTION_UP) {
	            	xCoor.setText("X: 0");
	            	Arkodroid.values[1] = "0";
	    			Arkodroid.sendStopValues(ArkodroidButtons.this);
	            }
				return false;
			}
	    });
		
		buzzerButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN) {
					if (System.currentTimeMillis() - lastchanged > Arkodroid.delay){
						Log.i(TAG, "Buzzer command sent");
			        	Amarino.sendDataToArduino(ArkodroidButtons.this, BluetoothConnect.DEVICE_ADDRESS, 'b', (1));
		            }
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					Log.i(TAG, "Buzzer stop command sent");
		        	Amarino.sendDataToArduino(ArkodroidButtons.this, BluetoothConnect.DEVICE_ADDRESS, 'b', (0));
	            }
				return false;
			}
	    });
    }
	
	public void onToggleClicked(View v) {
		Arkodroid.doLightToggleClick(this, v);
    }
	
	/** Hard "Back button" pressed **/
	@Override
    public void onBackPressed() {
		Arkodroid.exit(ArkodroidButtons.this);
    } 
    
	/** Menu creation **/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.connecttext:
            	startActivity(new Intent(this, BluetoothConnect.class));                
            	return true;
        	case R.id.musictext:
        		Arkodroid.selectMusic(this);
        		return true;
            case R.id.settingstext:
            	finish();
            	startActivity(new Intent(this, net.miguelmonteiro.android.arkodroid.Settings.class));
                return true;
            case R.id.abouttext:
                Arkodroid.aboutDialog(this);
                return true;
        }
        return true;
    }
}
